<?php

namespace app\manage\controller;

use app\common\controller\Manage;
use Request;
use app\common\model\Userpic as Userpics;
use app\common\model\MaterialCate as MaterialCat;
class UserPic extends Manage
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $userpics = new Userpics();
        if (Request::isAjax()) {
            $filter              = input('request.');
            return $userpics->tableData($filter);
        }
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function add()
    {
        $this->view->engine->layout(false);
        if(!Request::isPost())
        {
            //获取添加页面
            $parent = model('common/MaterialCate')->getMaterialTypeCate('user_pic');
            $this->assign('parent', $parent); //顶级分类
            return $this->fetch('add');
        }
        else
        {

            //存储添加内容
            $data = array(
                'cate_id' => input('cate_id'),
                'name' => input('name'),
                'image_id' => input('image_id'),
                'sort' => input('sort'),
                'marketable' => input('marketable'),
                'price'=>input('price')
            );

            $result = model('common/Syspic')->add($data);
            if($result !== false)
            {
                $return_data = array(
                    'status' => true,
                    'msg' => '添加成功',
                    'data' => $result
                );
            }
            else
            {
                $return_data = array(
                    'status' => false,
                    'msg' => '添加失败',
                    'data' => $result
                );
            }
            return $return_data;
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $this->view->engine->layout(false);
        if(!Request::isPost())
        {
            //获取添加页面
            $parent = model('common/MaterialCate')->getMaterialTypeCate('sys_pic');

            $this->assign('parent', $parent); //顶级分类

            $data = model('common/Syspic')->getFontInfo($id);
            $this->assign('data', $data); //分类信息
            return $this->fetch('edit');
        }
        else
        {
            //存储编辑内容
            $data = array(
                'id' => input('id'),
                'cate_id' => input('cate_id'),
                'name' => input('name'),
                'image_id' => input('image_id'),
                'sort' => input('sort'),
                'access_key' => input('access_key'),
                'marketable' => input('marketable')
            );
            $result = model('common/Syspic')->edit($data);
            return $result;
        }
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result     = [
            'status' => false,
            'msg'    => '关键参数丢失',
            'data'   => '',
        ];
        $userpics_id   = input("id");
        $userpics = new Userpics();
        if (!$userpics_id) {
            return $result;
        }
        $delRes = $userpics->delFont($userpics_id);
        if (!$delRes['status']) {
            $result['msg'] = $delRes['msg'];
            return $result;
        }
        $result['status'] = true;
        $result['msg']    = '删除成功';
        return $result;
    }



    /**
     * 批量删除蒙版
     * @return array
     */
    public function batchDel()
    {
        $result = [
            'status' => false,
            'data'   => [],
            'msg'    => '参数丢失',
        ];
        $ids    = input('ids/a', []);
        if (count($ids) <= 0) {
            return $result;
        }
        $userpics = new Userpics();
        foreach ($ids as $userpics_id) {
            $delRes = $userpics->delFont($userpics_id);
            if (!$delRes['status']) {
                $result['msg'] = $delRes['msg'];
                return $result;
            }
        }
        $result['status'] = true;
        $result['msg']    = '删除成功';
        return $result;
    }


    /**
     * 批量上下架
     * @return array
     */
    public function batchMarketable()
    {
        $result = [
            'status' => false,
            'data'   => [],
            'msg'    => '参数丢失',
        ];
        $ids    = input('ids/a', []);
        $type   = input('type/s', 'up');
        if (count($ids) <= 0) {
            return $result;
        }
        $userpics = new Userpics();
        $res        = $userpics->batchMarketable($ids, $type);
        if ($res !== false) {
            $result['status'] = true;
            $result['msg']    = '操作成功';
        } else {
            $result['msg'] = '操作失败';
        }
        return $result;
    }

}
